package stv5;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;


public class SeriesList implements Iterable<Series> {
	private HashMap<String, Series> seriesByName;	
	
	public SeriesList() {
		seriesByName = new HashMap<String, Series>();
	}

	public void add(Series news) {
		seriesByName.put(news.getName(), news);
	}
	
	public Collection<Series> managed() {
		Collection<Series> ret = new LinkedList<Series>();
		for (Series s : this)
			if (s.isManaged())
				ret.add(s);
		return ret;
	}

	@Override
	public Iterator<Series> iterator() {
		return seriesByName.values().iterator();
	}

	public int size() {
		return seriesByName.size();
	}

	public Series getByName(String name) {
		return seriesByName.get(name);
	}

	public Series getByName(Series s) {
		return seriesByName.get( s.getName() );
	}

	/**
	 * Collection of series, sorted by name
	 * @return
	 */
	public Collection<Series> sorted() {
		ArrayList<Series> ret = new ArrayList<Series>();
		ret.addAll( seriesByName.values() );
		Collections.sort(ret);
		return ret;
	}
}
